﻿using MaiPlanServer.Dto;
using MaiPlanServer.IServices.IEquipmentServices;
using MaiPlanServer.Model;
using MaiPlanServer.Utility;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MaiPlanServer.API.Controllers.Equipment
{

    [Route("api/[controller]")]
    [ApiController]
    public class EquipmentledgerController : ControllerBase
    {
        public EquipmentledgerController(IEquipmentledgerServices EmDb)
        {
            this.EmDb = EmDb;
        }
        public IEquipmentledgerServices EmDb { get; }

        /// <summary>
        /// 获取设备名称
        /// </summary>
        /// <param name="PageIndex">当前页数</param>
        /// <param name="PageSize">每页显示条数</param>
        /// <param name="mtName">规则名称</param>
        /// <param name="code">是否启用</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get(int PageIndex, int PageSize, string mtName, string code,string status)
        {
            try
            {
               
                var list = await EmDb.GetJsonAsync(PageIndex, PageSize, mtName, code, status);
                var count = await EmDb.GetCountAsync(mtName);

                return Ok(BaseState.CreateInstance(200, "请求成功",list, count));
            }
            catch (Exception ex)
            {
                return Ok(BaseState.CreateInstance(BaseState.RequestError, "请求失败ex"));
            }
        }

        /// <summary>
        /// 添加设备名称
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Add([FromForm] dv_machinery model)
        {
            try
            {
                if (!string.IsNullOrEmpty(model.machinery_name))
                {
                    model.machinery_id = Guid.NewGuid().ToString();
                    model.create_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    model.machinery_code = await EmDb.GetCodeasync();
                    model.workshop_id = "111111111111111";
                    var add = await EmDb.AddAsync(model);

                    return Ok(new { code = 200, msg = "请求成功" });
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return Ok(new { code = -1, msg = "请求失败", count = 0, data = "" });
        }

        /// <summary>
        /// 修改设备设置
        /// </summary>
        /// <param name="model">部门模型</param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> Put([FromForm] dv_machinery model)
        {
            try
            {
                model.update_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                var result = await EmDb.UpdateAsync(model);
                if (result)
                {
                    return Ok(BaseState.CreateInstance(200, "修改成功"));
                }
            }
            catch (System.Exception ex)
            {
                //后续写日志
            }
            return Ok(BaseState.CreateInstance(-1, "修改失败，服务器异常"));
        }

        /// <summary>
        /// 删除设备名称
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> Del(string Id)
        {
            try
            {
                var model = await EmDb.DellAsync(Id);
                if (model)
                {
                    return Ok(BaseState.CreateInstance(200, "删除成功"));
                }
                return Ok(BaseState.CreateInstance(-1, "删除失败"));
            }
            catch (Exception ex)
            {
                return Ok(BaseState.CreateInstance(-1, "删除失败"));
            }
        }


        /// <summary>
        /// 删除多条设备名称
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpDelete("DelAll")]
        public async Task<IActionResult> DelAll(string Id)
        {
            try
            {
                string[] Ids = Id.Split(',');
                var model = await EmDb.DellAllAsync(Ids);
                if (model)
                {
                    return Ok(BaseState.CreateInstance(200, "删除成功"));
                }
                return Ok(BaseState.CreateInstance(-1, "删除失败"));
            }
            catch (Exception ex)
            {
                return Ok(BaseState.CreateInstance(-1, "删除失败"));
            }
        }


    }
}
